package com.lanou.shunke.common.utils;

import com.alibaba.fastjson.JSON;
import com.lanou.shunke.common.entity.ShunkeResult;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;

public class JsoupUtils {

    public static ShunkeResult getData(String url) {
        Connection connect = Jsoup.connect(url);
        ShunkeResult shunkeResult = null;
        try {
            String data = connect.execute().body();

            System.out.println(data);
            shunkeResult = JSON.parseObject(data, ShunkeResult.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return shunkeResult;
    }

    public static ShunkeResult getData(String url, Map<String, Object> params) {
        String paramsStr = "";
        for (Map.Entry<String, Object> param : params.entrySet()) {
            paramsStr += param.getKey();
            paramsStr += "=";
            paramsStr += param.getValue();
            paramsStr += "&";
        }
        url = url + "?" + paramsStr;

        return getData(url);
    }

}
